<template>
  <div class="com-container" @dblclick="revertMap">
    <div class="com-chart" ref="mapRef"></div>
  </div>
</template>


<script>
import axios from "axios";
import { mapState } from "vuex";
import { getProvinceMapInfo } from "@/utils/map_utils";
export default {
  data() {
    return {
      myEchart: null,
      chinaMapData: null, //中国地图数据
      allData: null, //存放后端获取过来的数据
      titleFontSize: 0, //屏幕适配大小
      mapData: {}, //省份数据
    };
  },
  created() {
    this.$socket.registerCallBack("mapData", this.getData);
  },
  mounted() {
    this.initChart();
    // this.getData();
    this.$socket.send({
      action: "getData",
      socketType: "mapData",
      chartName: "map",
      value: "",
    });
    this.screenAdapter();
    window.addEventListener("resize", this.screenAdapter);
  },
  destroyed() {
    window.removeEventListener("resize", this.screenAdapter);
    this.$socket.unRegisterCallBack("mapData");
  },
  computed: {
    ...mapState(["theme"]),
  },
  watch: {
    theme() {
      //销毁当前图表
      this.myEchart.dispose();
      //以最新的主题初始化图表对象
      this.initChart();
      //屏幕适配
      this.screenAdapter();
      //渲染数据
      this.updateChart();
    },
  },
  methods: {
    async initChart() {
      //初始化
      this.myEchart = this.$echarts.init(this.$refs.mapRef, this.theme);
      //从本地获取地图数据
      const res = await axios.get(
        "http://localhost:8080/static/map/china.json"
      );
      this.$echarts.registerMap("china", res.data);
      const initOption = {
        geo: {
          type: "map",
          map: "china",
          itemStyle: {
            areaColor: "#2e72bf",
            borderColor: "#333",
          },
        },
      };
      this.myEchart.setOption(initOption);
      this.myEchart.on("click", async (arg) => {
        //将中文省份转换为拼音
        const provinceInfo = getProvinceMapInfo(arg.name);
        //如果mapData中没有缓存，再发起请求，并进行注册
        if (!this.mapData[provinceInfo.key]) {
          const res = await axios.get(
            "http://localhost:8080" + provinceInfo.path
          );
          //对矢量地图进行缓存
          this.mapData[provinceInfo.key] = res.data;
          //注册地图
          this.$echarts.registerMap(provinceInfo.key, res.data);
        }

        const changeOption = {
          geo: {
            map: provinceInfo.key,
          },
        };
        this.myEchart.setOption(changeOption);
        // console.log(res);
      });
    },
    //获取数据
    getData(res) {
      // const { data: res } = await this.$http.get("map");
      this.allData = res;
      this.updateChart();
      this.screenAdapter();
    },
    updateChart() {
      //图例的数据
      const legendArr = this.allData.map((item) => item.name);
      // console.log(this.titleFontSize);
      //散点图的数据
      const seriesArr = this.allData.map((item) => {
        return {
          type: "effectScatter",
          name: item.name,
          data: item.children,
          //让散点图使用地图坐标
          coordinateSystem: "geo",
          //涟漪动画效果配置
          rippleEffect: {
            //涟漪效果直径
            scale: 4,
            //设置为空心的涟漪效果
            brushType: "stroke",
          },
        };
      });
      const dataOption = {
        title: {
          text: "▎商家分布",
          top: "3%",
          left: "3%",
        },
        legend: {
          left: "2%",
          bottom: "5%",
          //图例的方向,垂直
          orient: "vertical",
          data: legendArr.reverse(),
        },
        series: seriesArr,
      };
      this.myEchart.setOption(dataOption);
    },
    screenAdapter() {
      this.titleFontSize = (this.$refs.mapRef.offsetWidth / 100) * 3;
      const adaptOption = {
        title: {
          textStyle: {
            fontSize: this.titleFontSize,
          },
        },
        legend: {
          itemWidth: this.titleFontSize,
          itemHeight: this.titleFontSize / 1.3,
          textStyle: {
            fontSize: this.titleFontSize / 1.2,
          },
        },
      };
      this.myEchart.setOption(adaptOption);
      this.myEchart.resize();
    },
    revertMap() {
      const revertOption = {
        geo: {
          map: "china",
        },
      };
      this.myEchart.setOption(revertOption);
    },
  },
};
</script>

<style lang="less" scoped>
</style>